package interfacesBeans;

import agenda.Contato;
import agenda.Email;
import agenda.FormaDeContato;
import agenda.Skype;
import agenda.Telefone;
import excecoes.DataInvalida;
import excecoes.FormaDeContatoInvalida;
import excecoes.NomeInvalido;
import javax.swing.JOptionPane;

public class AdicionarContato extends javax.swing.JPanel {

    private String textLabelErro = "";
    private String tipoFormaDeContato;

    public AdicionarContato() {
        initComponents();
        setInitialFieldValues();
        setFieldsEnderecoFormaDecontato();

    }

    public void setInitialFieldValues() {

        textFieldNome.setText("Digite o nome do contato");
        textFieldEndereco.setText("Digite o endereço do contato");
        textFieldDataAniversario.setText("01/01");

        radioButtonSkype.setSelected(false);
        radioButtonTelefone.setSelected(false);
        radioButtonEmail.setSelected(true);

        tipoFormaDeContato = "E-mail";

        textFieldComentario.setText("");
        labelErro.setText("");
    }

    public void setFieldsEnderecoFormaDecontato() {

        String textLabel, textField;

        if (radioButtonEmail.isSelected()) {
            textLabel = "E-mail";
            textField = "exemplo@gmail.com";
        } else if (radioButtonSkype.isSelected()) {
           textLabel = "Skype";
           textField = "SkypeName";
        } else {
            textLabel = "Telefone";
            textField = "031 83 8765 4321";
        }

        labelEnderecoContato.setText(textLabel);
        textFieldEnderecoContato.setText(textField);

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupFormadecontato = new javax.swing.ButtonGroup();
        labelAdicionarContato = new javax.swing.JLabel();
        labelNome = new javax.swing.JLabel();
        textFieldNome = new javax.swing.JTextField();
        labelEndereco = new javax.swing.JLabel();
        textFieldEndereco = new javax.swing.JTextField();
        labelDataAniversario = new javax.swing.JLabel();
        textFieldDataAniversario = new javax.swing.JTextField();
        labelFormadecontato = new javax.swing.JLabel();
        radioButtonSkype = new javax.swing.JRadioButton();
        radioButtonEmail = new javax.swing.JRadioButton();
        radioButtonTelefone = new javax.swing.JRadioButton();
        labelEnderecoContato = new javax.swing.JLabel();
        textFieldEnderecoContato = new javax.swing.JTextField();
        labelComentario = new javax.swing.JLabel();
        textFieldComentario = new javax.swing.JTextField();
        buttonAdicionar = new javax.swing.JButton();
        buttonCancelar = new javax.swing.JButton();
        labelErro = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        labelAdicionarContato.setFont(new java.awt.Font("Tahoma", 1, 28));
        labelAdicionarContato.setText("Adicionar Contato");

        labelNome.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelNome.setText("Nome");

        textFieldNome.setFont(new java.awt.Font("Tahoma", 0, 14));
        textFieldNome.setText("Digite o nome do contato");
        textFieldNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldNomeActionPerformed(evt);
            }
        });

        labelEndereco.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelEndereco.setText("Endereço");

        textFieldEndereco.setFont(new java.awt.Font("Tahoma", 0, 14));
        textFieldEndereco.setText("Digite o endereço do contato");

        labelDataAniversario.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelDataAniversario.setText("Data de aniversário");

        textFieldDataAniversario.setFont(new java.awt.Font("Tahoma", 0, 14));
        textFieldDataAniversario.setText("dd/MM");

        labelFormadecontato.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelFormadecontato.setText("Forma de contato");

        buttonGroupFormadecontato.add(radioButtonSkype);
        radioButtonSkype.setText("Skype");
        radioButtonSkype.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioButtonSkypeActionPerformed(evt);
            }
        });

        buttonGroupFormadecontato.add(radioButtonEmail);
        radioButtonEmail.setText("E-mail");
        radioButtonEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioButtonEmailActionPerformed(evt);
            }
        });

        buttonGroupFormadecontato.add(radioButtonTelefone);
        radioButtonTelefone.setText("Telefone");
        radioButtonTelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioButtonTelefoneActionPerformed(evt);
            }
        });

        labelEnderecoContato.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelEnderecoContato.setText("This is set.");

        textFieldEnderecoContato.setFont(new java.awt.Font("Tahoma", 0, 14));

        labelComentario.setFont(new java.awt.Font("Tahoma", 0, 14));
        labelComentario.setText("Comentário");

        textFieldComentario.setFont(new java.awt.Font("Tahoma", 0, 14));
        textFieldComentario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldComentarioActionPerformed(evt);
            }
        });

        buttonAdicionar.setText("Adicionar");
        buttonAdicionar.setPreferredSize(new java.awt.Dimension(80, 23));
        buttonAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAdicionarActionPerformed(evt);
            }
        });

        buttonCancelar.setText("Cancelar");
        buttonCancelar.setPreferredSize(new java.awt.Dimension(80, 23));
        buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelarActionPerformed(evt);
            }
        });

        labelErro.setFont(new java.awt.Font("Tahoma", 1, 14));
        labelErro.setForeground(new java.awt.Color(255, 0, 0));
        labelErro.setText("Mensagens de erro");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelAdicionarContato)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(labelEndereco)
                            .addComponent(labelNome)
                            .addComponent(labelDataAniversario)
                            .addComponent(labelFormadecontato))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(textFieldEndereco, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(textFieldDataAniversario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(textFieldNome, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 502, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(radioButtonSkype)
                                                .addComponent(radioButtonEmail))
                                            .addGap(62, 62, 62)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(buttonAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addGap(23, 23, 23))
                                                .addComponent(labelEnderecoContato)))
                                        .addComponent(labelComentario))
                                    .addComponent(radioButtonTelefone))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(buttonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(textFieldEnderecoContato, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                                            .addComponent(textFieldComentario, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))))))))
                .addGap(181, 181, 181))
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(labelErro, javax.swing.GroupLayout.PREFERRED_SIZE, 531, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(246, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonAdicionar, buttonCancelar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(labelAdicionarContato)
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(textFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textFieldEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelEndereco))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelDataAniversario)
                            .addComponent(textFieldDataAniversario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(radioButtonEmail)
                                .addGap(18, 18, 18)
                                .addComponent(radioButtonSkype))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(63, 63, 63)
                                .addComponent(labelFormadecontato)
                                .addGap(14, 14, 14)))
                        .addGap(18, 18, 18)
                        .addComponent(radioButtonTelefone))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelEnderecoContato)
                            .addComponent(textFieldEnderecoContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelComentario)
                            .addComponent(textFieldComentario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(23, 23, 23)))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addComponent(labelErro, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buttonAdicionar, buttonCancelar});

    }// </editor-fold>//GEN-END:initComponents

    private void textFieldNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldNomeActionPerformed

    }//GEN-LAST:event_textFieldNomeActionPerformed

    private void radioButtonEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonEmailActionPerformed
        tipoFormaDeContato = "E-mail";
        labelEnderecoContato.setText("E-mail");
        textFieldEnderecoContato.setText("exemplo@gmail.com");
    }//GEN-LAST:event_radioButtonEmailActionPerformed

    private void buttonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelarActionPerformed
        textLabelErro = "";
        setInitialFieldValues();
        java.awt.CardLayout layout = (java.awt.CardLayout)Body.panelBody.getLayout();
        layout.show(Body.panelBody, "Contatos");
    }//GEN-LAST:event_buttonCancelarActionPerformed

    private void buttonAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAdicionarActionPerformed

        textLabelErro = "";

        String nome = textFieldNome.getText();
        String endereco = textFieldEndereco.getText();
        String dataFormatada = textFieldDataAniversario.getText();

        String enderecoContato = textFieldEnderecoContato.getText();
        String comentario = textFieldComentario.getText();
        FormaDeContato f;

        try {
            if (tipoFormaDeContato == "E-mail") {
                f = new Email(enderecoContato, comentario);
            } else if (tipoFormaDeContato == "Skype") {
                f = new Skype(enderecoContato, comentario);
            } else {
                f = new Telefone(enderecoContato, comentario);
            }

            Contato c = new Contato(nome, endereco, dataFormatada, f);
            Body.user.addContato(c);

            javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel)Body.contatos.tableContatos.getModel();
            model.setNumRows(0);

            for (Contato con : Body.user.getAgenda().getContatos()) {
                model.addRow(new String[] {con.getNome(), con.getDataAniversarioFormatada()});
            }

            JOptionPane.showMessageDialog(this, "O contato foi adicionado à agenda!", "Contato adicionado", JOptionPane.INFORMATION_MESSAGE);

            java.awt.CardLayout layout = (java.awt.CardLayout) Body.panelBody.getLayout();
            layout.show(Body.panelBody, "Contatos");

        } catch (FormaDeContatoInvalida fi) {
            textLabelErro = fi.getMessage();
        } catch (NomeInvalido n) {
            textLabelErro = n.getMessage();
        }  catch (DataInvalida d) {
            textLabelErro = d.getMessage();
        }

        labelErro.setText(textLabelErro);

    }//GEN-LAST:event_buttonAdicionarActionPerformed

    private void radioButtonSkypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonSkypeActionPerformed
        tipoFormaDeContato = "Skype";
        labelEnderecoContato.setText("Skype");
        textFieldEnderecoContato.setText("SkypeName");
    }//GEN-LAST:event_radioButtonSkypeActionPerformed

    private void radioButtonTelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioButtonTelefoneActionPerformed
        tipoFormaDeContato = "Telefone";
        labelEnderecoContato.setText("Telefone");
        textFieldEnderecoContato.setText("031 83 8765 4321");
    }//GEN-LAST:event_radioButtonTelefoneActionPerformed

    private void textFieldComentarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldComentarioActionPerformed

    }//GEN-LAST:event_textFieldComentarioActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdicionar;
    private javax.swing.JButton buttonCancelar;
    private javax.swing.ButtonGroup buttonGroupFormadecontato;
    private javax.swing.JLabel labelAdicionarContato;
    private javax.swing.JLabel labelComentario;
    private javax.swing.JLabel labelDataAniversario;
    private javax.swing.JLabel labelEndereco;
    private javax.swing.JLabel labelEnderecoContato;
    private javax.swing.JLabel labelErro;
    private javax.swing.JLabel labelFormadecontato;
    private javax.swing.JLabel labelNome;
    private javax.swing.JRadioButton radioButtonEmail;
    private javax.swing.JRadioButton radioButtonSkype;
    private javax.swing.JRadioButton radioButtonTelefone;
    private javax.swing.JTextField textFieldComentario;
    private javax.swing.JTextField textFieldDataAniversario;
    private javax.swing.JTextField textFieldEndereco;
    private javax.swing.JTextField textFieldEnderecoContato;
    private javax.swing.JTextField textFieldNome;
    // End of variables declaration//GEN-END:variables
}
